'''
Created on 2010-8-17

@author: madlee
'''

from OpenGL.GL import *
from OpenGL.GLUT import *
import wx

from kuai.xyz import XYZ
from kuai.mol import Element

   
def atom_color(number):
    if number == 1:
        return wx.Color(255, 255, 255)
    elif number == 6:
        return wx.Color(0, 255, 0)
    elif number == 7:
        return wx.Color(0, 0, 255)
    elif number == 8:
        return wx.Color(255, 0, 0)
    elif number == 16:
        return wx.Color(255, 255, 0)
    else:
        return wx.Color(128, 128, 128)
        

class Glyph:
    def __init__(self, parent):
        self.children = []
        self.parent = parent
        if parent:
            parent.children.append(self)
    
    def draw(self):
        for i in self.children:
            i.draw()
        
class AtomGlyph(Glyph):
    def __init__(self, parent):
        Glyph.__init__(self, parent)
        self.symbol = 'C'
        self.coord = XYZ(0.0, 0.0, 0.0)
    
    @property
    def number(self):
        return Element.get(self.symbol).number
    
    @property
    def color(self):
        try:
            return self.__color
        except:
            self.__color = atom_color(self.number) 
            return self.__color 
       

class AtomGlyphP(AtomGlyph):
    def __init__(self, parent):
        AtomGlyph.__init__(self, parent)

    def draw(self):
        # glBegin(GL_POINTS)
        # glColor3b(self.color.red, self.color.green, self.color.blue)
        # glVertex3d(self.coord.x, self.coord.y, self.coord.z)
        # glEnd()
        # AtomGlyph.draw(self)
        pass
        
class AtomGlpyhBall(AtomGlyph):
    def draw(self):
        glBegin(GL_POINTS)
        glColor3b(self.color.red, self.color.green, self.color.blue)
        print self.color.red, self.color.green, self.color.blue
        glVertex3d(self.coord.x, self.coord.y, self.coord.z)
        glEnd()
        AtomGlyph.draw(self)
    
class BondGlyph(Glyph):
    def __init__(self, atom1, atom2):
        Glyph.__init__(self)
        self.atom1 = atom1
        self.atom2 = atom2
        
class BondGlyphL(BondGlyph):
    def __init__(self, parent, atom1, atom2):
        Glyph.__init__(self, parent)
        self.atom1 = atom1
        self.atom2 = atom2

    def draw(self):
        glBegin(GL_LINES)
        glColor3f(self.atom1.color.red/255.0, self.atom1.color.green/255.0, self.atom1.color.blue/255.0)
        
        glVertex3d(self.atom1.coord.x, self.atom1.coord.y, self.atom1.coord.z)
        glColor3f(self.atom2.color.red/255.0, self.atom2.color.green/255.0, self.atom2.color.blue/255.0)
        glVertex3d(self.atom2.coord.x, self.atom2.coord.y, self.atom2.coord.z)
        glEnd()
        BondGlyph.draw(self)

